{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "from __future__ import print_function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def create_examples(N, batch_size):\n",
    "    A = np.random.binomial(n=1, p=0.5, size=(batch_size, N))\n",
    "    B = np.random.binomial(n=1, p=0.5, size=(batch_size, N,))\n",
    "\n",
    "    X = np.zeros((batch_size, 2 *N,), dtype=np.float32)\n",
    "    X[:,:N], X[:,N:] = A, B\n",
    "\n",
    "    Y = (A ^ B).astype(np.float32)\n",
    "    return X,Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tf_rl.models import MLP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tf.ops.reset_default_graph()\n",
    "sess = tf.InteractiveSession()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "N = 5\n",
    "# we add a single hidden layer of size 12\n",
    "# otherwise code is similar to above\n",
    "HIDDEN_SIZE = 12\n",
    "\n",
    "x = tf.placeholder(tf.float32, (None, 2 * N), name=\"x\")\n",
    "y_golden = tf.placeholder(tf.float32, (None, N), name=\"y\")\n",
    "\n",
    "mlp = MLP(2 * N, [HIDDEN_SIZE, N], [tf.tanh, tf.sigmoid])\n",
    "y = mlp(x)\n",
    "\n",
    "cost = tf.reduce_mean(tf.square(y - y_golden))\n",
    "\n",
    "optimizer = tf.train.AdagradOptimizer(learning_rate=0.3)\n",
    "train_op = optimizer.minimize(cost)\n",
    "sess.run(tf.initialize_all_variables())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mlp2 = mlp.copy(sess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.25368\n",
      "0.24415\n",
      "0.170396\n",
      "0.0975643\n",
      "0.0823987\n",
      "0.0314237\n",
      "0.0417636\n",
      "0.0364044\n",
      "0.039408\n",
      "0.026788\n"
     ]
    }
   ],
   "source": [
    "for t in range(5000):\n",
    "    example_x, example_y = create_examples(N, 10)\n",
    "    cost_t, _ = sess.run([cost, train_op], {x: example_x, y_golden: example_y})\n",
    "    if t % 500 == 0: \n",
    "        print(cost_t.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy over 1000 examples: 99 %\n"
     ]
    }
   ],
   "source": [
    "N_EXAMPLES = 1000\n",
    "example_x, example_y = create_examples(N, N_EXAMPLES)\n",
    "is_correct = tf.less_equal(tf.abs(y - y_golden), tf.constant(0.5))\n",
    "accuracy = tf.reduce_mean(tf.cast(is_correct, \"float\"))\n",
    "\n",
    "acc_result = sess.run(accuracy, {x: example_x, y_golden: example_y})\n",
    "print(\"Accuracy over %d examples: %.0f %%\" % (N_EXAMPLES, 100.0 * acc_result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy over 1000 examples: 52 %\n"
     ]
    }
   ],
   "source": [
    "# If copy works accuracy should be around 50% for this one\n",
    "N_EXAMPLES = 1000\n",
    "example_x, example_y = create_examples(N, N_EXAMPLES)\n",
    "is_correct = tf.less_equal(tf.abs(mlp2(x) - y_golden), tf.constant(0.5))\n",
    "accuracy = tf.reduce_mean(tf.cast(is_correct, \"float\"))\n",
    "\n",
    "acc_result = sess.run(accuracy, {x: example_x, y_golden: example_y})\n",
    "print(\"Accuracy over %d examples: %.0f %%\" % (N_EXAMPLES, 100.0 * acc_result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
